/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerTicketsView.java
 *
 * Created on Apr 4, 2010, 2:03:41 AM
 */
package ticketmaster.presentation.customer;

import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JTable;
import ticketmaster.businessobject.SeatBO;
import ticketmaster.businessobject.Ticket;
import ticketmaster.common.SeatCategories;
import ticketmaster.dao.DAOFactory;
import ticketmaster.dataobject.BookedTicket;
import ticketmaster.dataobject.TicketRate;
import ticketmaster.dataobject.TicketView;
import ticketmaster.presentation.customer.purchase.ShippingMethodsView;

/**
 *
 * @author Wei Liu
 */
public class CustomerTicketsView extends CustomerBasePanel
{

    /** Creates new form CustomerTicketsView */
    public CustomerTicketsView()
    {
        initComponents();
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnPay = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(CustomerTicketsView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, -1, -1));

        btnPay.setFont(resourceMap.getFont("btnPay.font")); // NOI18N
        btnPay.setText(resourceMap.getString("btnPay.text")); // NOI18N
        btnPay.setName("btnPay"); // NOI18N
        btnPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPayActionPerformed(evt);
            }
        });
        add(btnPay, new org.netbeans.lib.awtextra.AbsoluteConstraints(23, 560, 220, 50));
    }// </editor-fold>//GEN-END:initComponents

    private void btnPayActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPayActionPerformed
    {//GEN-HEADEREND:event_btnPayActionPerformed
        JTable dataTable = table.getTable();

        this.getCart().clear();
        bookedTickets = new ArrayList<BookedTicket>();

        for (int i = 0; i < dataTable.getRowCount(); i++)
        {
            TicketView ticketView = (TicketView) dataTable.getValueAt(i, 4);
            if (ticketView.getTransactionID() == 0 && Boolean.parseBoolean(dataTable.getValueAt(i, 6).toString()))
            {
                Ticket ticket = new Ticket();
                TicketRate rate = new TicketRate();
                rate.setEventID(ticketView.getEventID());
                rate.setVenueID(ticketView.getVenueID());
                rate.setSeatCategoryID(SeatCategories.getSeatCategory(ticketView.getSeatCategory()));
                rate.setPrice(ticketView.getPrice());
                ticket.setTicketRate(rate);
                ticket.setCustomerID(this.getUser().getUserID());
                ticket.setBookedDate(ticketView.getBookedDate());
                ticket.setIsCancelled(ticketView.isIsCancelled());
                ticket.setIsPaid(ticketView.isIsPaid());

                SeatBO seat = new SeatBO();
                seat.setPrice(ticketView.getPrice());
                seat.setSeatNumber(ticketView.getSeatNumber());
                ticket.setSeat(seat);

                this.getCart().add(ticket);

                BookedTicket bookedTicket = new BookedTicket();
                bookedTicket.setBookedDate(ticketView.getBookedDate());
                bookedTicket.setBookedTicketID(ticketView.getBookedTicketID());
                bookedTicket.setCustomerID(ticketView.getCustomerID());
                bookedTicket.setEventID(ticketView.getEventID());
                bookedTicket.setIsCancelled(ticketView.isIsCancelled());
                bookedTicket.setIsPaid(ticketView.isIsPaid());
                bookedTicket.setSeatCategoryID(SeatCategories.getSeatCategory(ticketView.getSeatCategory()));
                bookedTicket.setSeatNumber(ticketView.getSeatNumber());
                bookedTicket.setVenueID(ticketView.getVenueID());

                bookedTickets.add(bookedTicket);
            }
        }

        directToShippingMethodsView();
    }//GEN-LAST:event_btnPayActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPay;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    private TicketsTable table;
    private Collection<TicketView> tickets;
    private Collection<BookedTicket> bookedTickets;

    private void initTable()
    {
        table = new TicketsTable(getTickets());
        add(table, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));
    }

    private void directToShippingMethodsView()
    {
        if (bookedTickets.size() != 0)
        {
            ShippingMethodsView smv = new ShippingMethodsView();
            smv.setCart(this.getCart());
            smv.setBookedTickets(bookedTickets);
            smv.setPnlParent(this.getPnlParent());
            this.getPnlParent().removeAll();
            this.getPnlParent().add(smv);
            this.getPnlParent().validate();
            this.getPnlParent().repaint();
        }
    }

    /**
     * @return the tickets
     */
    public Collection<TicketView> getTickets()
    {
        if (tickets == null)
        {
            tickets = DAOFactory.getInstance().getTicketViewDAO().getAllTicketViewDO(this.getUser().getUserID());
        }
        return tickets;
    }

    /**
     * @param tickets the tickets to set
     */
    public void setTickets(Collection<TicketView> tickets)
    {
        this.tickets = tickets;
    }
}
